package com.example.glide_test.request;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.widget.ImageView;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.LinkedBlockingDeque;


//执行请求线程
public class BitmapDispatch extends Thread{
    //请求队列
    private LinkedBlockingDeque<BitmapRequest> mQueue;
    private Handler handler = new Handler(Looper.getMainLooper());

    public BitmapDispatch(LinkedBlockingDeque<BitmapRequest> mQueue) {
        this.mQueue = mQueue;
    }

    @Override
    public void run() {
        super.run();
        while (true){
            try {
                //获取请求对象
                BitmapRequest bitmapRequest = mQueue.take();
                //显示占位图片
                showLoadingImage(bitmapRequest);
                //加载图片
                Bitmap bitmap = findBitmap(bitmapRequest);
                showImageView(bitmapRequest,bitmap);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void showImageView(BitmapRequest bitmapRequest, Bitmap bitmap) {
        if (bitmap != null && bitmapRequest.getImageView() != null &&
        bitmapRequest.getImageView().getTag().equals(bitmapRequest.getUrlMD5())){
            ImageView imageView = bitmapRequest.getImageView();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    imageView.setImageBitmap(bitmap);
                }
            });
        }
    }

    private Bitmap findBitmap(BitmapRequest bitmapRequest) {
        return downLoadImage(bitmapRequest.getUrl());
    }

    private void showLoadingImage(BitmapRequest bitmapRequest) {
        if (bitmapRequest.getResId()>0 &&bitmapRequest.getImageView() != null){
            int resId = bitmapRequest.getResId();
            ImageView imageView = bitmapRequest.getImageView();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    imageView.setImageResource(resId);
                }
            });
        }
    }

    private Bitmap downLoadImage(String url) {
        InputStream is = null;
        Bitmap bitmap = null;
        try {
            URL url1 = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
            is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (is != null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }

}
